package com.bcmusic.user.controller;

import com.bcmusic.user.config.TokenDecode;
import com.bcmusic.user.feign.UserFeign;
import com.bcmusic.user.pojo.MusicianIdentification;
import com.bcmusic.user.pojo.User;
import com.bcmusic.user.service.MusicianIdentificationService;
import com.bcmusic.user.service.UserService;
import com.github.pagehelper.PageInfo;
import entity.IdWorker;
import entity.Message;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/musicianIdentification")
@CrossOrigin
public class MusicianIdentificationController {

    @Autowired
    private MusicianIdentificationService musicianIdentificationService;

    @Autowired
    private UserService userService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TokenDecode tokenDecode;

    /***
     * MusicianIdentification分页条件搜索实现
     * @param musicianIdentification
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) MusicianIdentification musicianIdentification, @PathVariable int page, @PathVariable int size) {
        //调用MusicianIdentificationService实现分页条件查询MusicianIdentification
        PageInfo<MusicianIdentification> pageInfo = musicianIdentificationService.findPage(musicianIdentification, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * MusicianIdentification分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用MusicianIdentificationService实现分页查询MusicianIdentification
        PageInfo<MusicianIdentification> pageInfo = musicianIdentificationService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param musicianIdentification
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<MusicianIdentification>> findList(@RequestBody(required = false) MusicianIdentification musicianIdentification) {
        //调用MusicianIdentificationService实现条件查询MusicianIdentification
        List<MusicianIdentification> list = musicianIdentificationService.findList(musicianIdentification);
        return new Result<List<MusicianIdentification>>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        //调用MusicianIdentificationService实现根据主键删除
        musicianIdentificationService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 修改MusicianIdentification数据
     * @param musicianIdentification
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody MusicianIdentification musicianIdentification, @PathVariable Long id) {
        //设置主键值
        musicianIdentification.setIdentificationId(id);
        //调用MusicianIdentificationService实现修改MusicianIdentification
        musicianIdentificationService.update(musicianIdentification);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 新增MusicianIdentification数据
     * @param musicianIdentification
     * @return
     */
    @PostMapping
    public Result add(@RequestBody MusicianIdentification musicianIdentification) {
        //调用MusicianIdentificationService实现添加MusicianIdentification
        musicianIdentificationService.add(musicianIdentification);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询MusicianIdentification数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<MusicianIdentification> findById(@PathVariable Long id) {
        //调用MusicianIdentificationService实现根据主键查询MusicianIdentification
        MusicianIdentification musicianIdentification = musicianIdentificationService.findById(id);
        return new Result<MusicianIdentification>(true, StatusCode.OK, "查询成功", musicianIdentification);
    }

    /***
     * 查询MusicianIdentification全部数据
     * @return
     */
    @GetMapping
    public Result<List<MusicianIdentification>> findAll() {
        //调用MusicianIdentificationService实现查询所有MusicianIdentification
        List<MusicianIdentification> list = musicianIdentificationService.findAll();
        return new Result<List<MusicianIdentification>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 独立音乐人的认证
     * 这个后期应该可以用泛型来搞。。。。。暂时想不出
     */
    @Transactional
    @PostMapping("authentication")
    public Result<MusicianIdentification> authentication(@RequestBody MusicianIdentification musicianIdentification) {
        String userNum = tokenDecode.getUserInfo().get("id");
        User user = userService.findUserByNum(userNum);
        String photo=null;
        if (user.getUserIdentiId() != 0) {
            MusicianIdentification old = new MusicianIdentification();
            old.setIdentificationId(user.getUserIdentiId());
            old.setValidStatus(-2);
            old=musicianIdentificationService.updateSelective(musicianIdentification);
            photo=old.getProfilePhoto();
        }
        musicianIdentification.setMusicianNumber(userNum);
        musicianIdentification.setSubmitTime(new Date());
        musicianIdentification.setSubmitterNumber(userNum);
        musicianIdentification.setProfilePhoto(photo);
        musicianIdentification = musicianIdentificationService.add(musicianIdentification);
        user.setUserIdentiId(musicianIdentification.getIdentificationId());
        user.setUserType(1);
        userService.update(user);
        musicianIdentification = musicianIdentificationService.findById(musicianIdentification.getIdentificationId());
        return new Result<>(true, StatusCode.OK, "认证成功", musicianIdentification);
    }
}
